﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace WarPigs
{
    public class Camera
    {
        private float max_pitch;
        private float min_pitch;
        private float z_plane;

        public float Distance;

        public Camera(float max_pitch, float min_pitch, float z_plane, float distance)
        {
            this.max_pitch = max_pitch;
            this.min_pitch = min_pitch;
            this.z_plane = z_plane;
            this.Distance = distance;
            pitch = 52.0f; //min_pitch
        }

        public Vector3 Focus = new Vector3(0, 0, 0);
         
        private float yaw;
        public float pitch;

        public void Orbit(float f)
        { 
            yaw += f;
            if (yaw < 0) yaw += 360;
            if (yaw >= 360) yaw -= 360;
        }

        public void Pitch(float f)
        {
            pitch += f;
            if (pitch < min_pitch) pitch = min_pitch;
            if (pitch > max_pitch) pitch = max_pitch;
        }

         public void Pan(float X, float Y, float speed)
         {
            Vector3 camera_shift_y = new Vector3(0, speed, 0);

            camera_shift_y = Vector3.Transform(camera_shift_y,
                Matrix.CreateRotationZ(MathHelper.ToRadians(-yaw)));
            Vector3 camera_shift_x = Vector3.Transform(camera_shift_y,
                Matrix.CreateRotationZ(MathHelper.ToRadians(90)));

            if (Math.Abs(X) > 0.1f) 
                Focus -= camera_shift_x * X;
            if (Math.Abs(Y) > 0.1f) 
                Focus += camera_shift_y * Y;

            Focus.Z = z_plane;

         }

        public Matrix ViewMatrix
        {
            get {
                Matrix m = Matrix.CreateTranslation(0, 0, -Distance);
                m = Matrix.CreateRotationX(MathHelper.ToRadians(-pitch)) * m;
                m = Matrix.CreateRotationZ(MathHelper.ToRadians(yaw)) * m;
                m = Matrix.CreateTranslation(-Focus) * m;

                return m; }
        }
    }
}
